home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / goodies / chromakeymovie / menu.c < prev    next >
Encoding:
Text File  |  2000-09-28  |  11.9 KB  |  473 lines

  1. /*
  2.     File:        menu.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: Jason Hodges-Harris    
  7.  
  8.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/28/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. // Mac Toolbox headers
  25.  
  26. #ifndef __DESK__
  27. #include <Desk.h>
  28. #endif
  29.  
  30. #ifndef __ERRORS__
  31. #include <Errors.h>
  32. #endif
  33.  
  34. #ifndef __MEMORY__
  35. #include <Memory.h>
  36. #endif
  37.  
  38. #ifndef __MENUS__
  39. #include <Menus.h>
  40. #endif
  41.  
  42. #ifndef __QUICKDRAW__
  43. #include <QuickDraw.h>
  44. #endif
  45.  
  46. #ifndef __TOOLUTILS__
  47. #include <ToolUtils.h>
  48. #endif
  49.  
  50. #ifndef __WINDOWS__
  51. #include <Windows.h>
  52. #endif
  53.  
  54.  
  55. // Program headers
  56.  
  57. #ifndef __CHROMAPPHEADER__
  58. #include "ChromaKeyMovie.h"
  59. #endif
  60.  
  61.  
  62.  
  63. //    Global Variables
  64.  
  65. extern short            gKeyMode;
  66. extern Boolean            gDone;                // program loop test condition
  67. extern Boolean            gMovieOpen;
  68. extern Boolean            gMovieBackGrnd;
  69.  
  70.  
  71. extern GWorldPtr        gOffscreenPort,
  72.                         gBackGroundPort,
  73.                         gBackGroundPicture;
  74. extern PixMapHandle        gMoviePixmap,
  75.                         gBackGndPixmap,
  76.                         gBackGndPictPM;
  77.  
  78.  
  79. // Initialise the application menubar.
  80.  
  81. #pragma segment Menu
  82. void MenuBarInit (void)
  83. {
  84.     SetMenuBar (GetNewMBar(rMenuBar));
  85.     AppendResMenu (GetMenuHandle(mApple),'DRVR');
  86.     DrawMenuBar();
  87. }
  88.  
  89.  
  90. // Sets the menubar to the default settings during the application initialisation phase.
  91. // i.e. In the 'Keying Mode' menu, the 'Transparent Color Transfer' menu option has a
  92. // tick mark against it to indicate it as the currently selected mode.
  93.  
  94. #pragma segment Menu
  95. void    DoAdjustMenus(void)
  96. {
  97.     DoAdjustFileMenu();
  98.     DoAdjustEditMenu();
  99.     DoAdjustKeyMode();
  100.     DoAdjustOptions(false,true);    // set the default to loop movie and not play every frame
  101.     DrawMenuBar();
  102.     return;
  103. }
  104.  
  105.  
  106.  
  107. // The DoMenuCommand() function performs all of the handling of 
  108. // the user's interaction with the application
  109.  
  110. #pragma segment Menu
  111. void    DoMenuCommand(long menuResult)
  112. {
  113.     MovieDocHndl        theDocHandle;
  114.     Str255                daName;
  115.     WindowPtr            window,theNextWindow;
  116.     short                menuID,
  117.                         menuItem,
  118.                         daRefNum;
  119.     static Boolean        sLoopMovie = true;            // set the movie to loop as default
  120.     static Boolean        sPlayAllFrames = false;        // set don't play all frames as default
  121.     
  122.     menuID   = HiWord(menuResult);
  123.     menuItem = LoWord(menuResult);
  124.  
  125.     switch (menuID) 
  126.     {
  127.  
  128.         case mApple:                // Apple menubar items
  129.             switch (menuItem) 
  130.             {
  131.                 /* Display the application about box.
  132.                    App name, copyright etc. */
  133.                 case iAbout:        
  134.                     DisplayAlert (rAboutBox,0,0);
  135.                 break;
  136.  
  137.                 // handle all menubar Desk Accessories.
  138.                 default:            
  139.                     GetMenuItemText(GetMenuHandle(mApple), menuItem, daName);
  140.                     daRefNum = OpenDeskAcc(daName);
  141.                 break;
  142.             }
  143.             break;
  144.  
  145.         case mFile:                        // File menubar items
  146.             switch (menuItem) 
  147.             {
  148.                 case iOpen:                // Open movie file
  149.                 /* Do nothing if a window is already open.
  150.                    Currently this application can only handle
  151.                    one open window manily due to processor 
  152.                    requirements in keeping a movie serviced
  153.                    at a high frame rate. */
  154.                 if (!gMovieOpen)
  155.                     {
  156.                          /* initialise environment for movie playback.
  157.                             If an error is encountered, the PlayMovieChroma()
  158.                             function will return false and the application will
  159.                             bail without displaying an error as it should have
  160.                             recovered gracefully. */
  161.                          if (PlayMovieChroma()) 
  162.                         {
  163.                             gMovieOpen = true;    // set boolean to indicate movie window open
  164.                             /* Perform tests to see if menu items to loop and play all
  165.                               frames of the movie are selected and set the movie according */
  166.                             if (sPlayAllFrames)
  167.                                 sPlayAllFrames = SetPlayAllFrames(!sPlayAllFrames);
  168.                             if (sLoopMovie)
  169.                                 sLoopMovie = SetLoopMovie(!sLoopMovie);
  170.  
  171.                             /* Test for which croma key mode is selected and set
  172.                                if the default Transparent Color Transfer mode isn't used. */
  173.                             if (gKeyMode == graphix)
  174.                                 VideoGraphicsMode(FrontWindow(),true);
  175.                             if (gKeyMode == modifierTrax)
  176.                                 ModifierTrackMode(FrontWindow());
  177.                             DoAdjustFileMenu();
  178.                         }
  179.                     }
  180.                 break;
  181.                 case iClose:
  182.                     if (gMovieOpen)
  183.                     {
  184.                         /* close open movie window and reset boolean to indicate
  185.                            that another window can be opened */
  186.                         gMovieOpen = false;
  187.                         DisposeWindowDocs (FrontWindow());
  188.                     }
  189.                 break;
  190.                 case iQuit:
  191.                     /* As quit menu option selected, close the window (if open),
  192.                        and set the loop boolean to true to allow application to exit. */
  193.                     if (gMovieOpen)
  194.                     {
  195.                         window = FrontWindow();
  196.                         while (window)
  197.                         {
  198.                             theNextWindow = &((WindowPeek)window)->nextWindow->port;
  199.                             DisposeWindowDocs (window);
  200.                             window = theNextWindow;
  201.                         }
  202.                         // SetPort(FrontWindow());
  203.                     }
  204.                     gMovieOpen = false;
  205.                     gDone=true;
  206.                 break;
  207.             }
  208.             break;
  209.  
  210.         /*  Edit menubar options. These aren't
  211.             implemented but included for completeness. */
  212.         case mEdit:        
  213.             switch (menuItem) 
  214.             {
  215.                 case iUndo:
  216.                     break;
  217.                 case iCut:
  218.                     break;
  219.                 case iCopy:
  220.                     break;
  221.                 case iPaste:
  222.                     break;
  223.             }
  224.             DoAdjustEditMenu();
  225.         break;
  226.         /* The (Keying Mode) mMode menu, controls the user interaction
  227.            with the different methods of keying a movie 
  228.            and setting up the display options. */
  229.         case mMode:
  230.                 
  231.             window = FrontWindow();
  232.             switch (menuItem)
  233.             {
  234.                 case iKeyColor:
  235.                     /* Sets the color used as transparent to allow the background
  236.                      image to display thro the foreground image and updates
  237.                      the modifier track / graphix modes if either is the selected
  238.                      mode, as these only set the transparent color when initialised. */
  239.                     TransparentColor();    
  240.                     switch (gKeyMode)
  241.                     {
  242.                         case modifierTrax:
  243.                             // update the modifier track information
  244.                         break;
  245.                         case graphix:
  246.                             // update the graphix mode to key out new color
  247.                             VideoGraphicsMode(window,true);    
  248.                         break;
  249.                     }
  250.                 break;
  251.                 /* Set the gMovieBackGrnd variable to position the movie in front or behind of the 
  252.                    background image when using the 'Transparent Color Transfer' mode */
  253.                 case iMovieBack:
  254.                     gMovieBackGrnd = true;
  255.                 break;
  256.                 case iMovieFront:
  257.                     gMovieBackGrnd = false;
  258.                 break;
  259.                 /* 'Transparent Color Transfer' mode selected. If a window isn't open
  260.                     just set the transfer mode global, else check the current
  261.                     mode of the movie window and remove information set by previous mode. */
  262.                 case iTransparent:
  263.                     if (gMovieOpen)
  264.                     {
  265.                         theDocHandle=(MovieDocHndl)GetWRefCon((WindowPtr)window);
  266.                         switch (gKeyMode)
  267.                         {
  268.                             case modifierTrax:
  269.                                 HLock((Handle)theDocHandle);
  270.                                 if (DestroyModifierTrack(theDocHandle))
  271.                                     DisplayAlert (rGenAlert,rErrMessages,4);
  272.                                 HUnlock((Handle)theDocHandle);
  273.                             break;
  274.                             case graphix:
  275.                                 VideoGraphicsMode(window,false);
  276.                             break;
  277.                             
  278.                         }
  279.                         SetMovieGWorld((**theDocHandle).theMovie,gOffscreenPort,nil);
  280.                     }
  281.                     gKeyMode = transparentMode;
  282.                 break;
  283.                 /* 'Graphix Mode' mode selected. If a window isn't open
  284.                     just set the transfer mode global, else check the current
  285.                     mode of the movie window and remove information set by previous mode. */
  286.                 case iGraphix:
  287.                     if (gMovieOpen)
  288.                     {
  289.                         theDocHandle=(MovieDocHndl)GetWRefCon((WindowPtr)window);
  290.                         switch (gKeyMode)
  291.                         {
  292.                             case modifierTrax:
  293.                                 HLock((Handle)theDocHandle);
  294.                                 if (DestroyModifierTrack(theDocHandle))
  295.                                     DisplayAlert (rGenAlert,rErrMessages,4);
  296.                                 HUnlock((Handle)theDocHandle);
  297.                             break;
  298.                         }
  299.                         VideoGraphicsMode(window,true);    // use VideoMediaGraphicsMode. See IM QuickTime 2:287
  300.                     }
  301.                     gKeyMode = graphix;
  302.                 break;
  303.                 /* 'Modifier Track' mode selected. If a window isn't open
  304.                     just set the transfer mode global, else check the current
  305.                     mode of the movie window and remove information set by previous mode. */
  306.                 case iModifier:
  307.                     if (gMovieOpen)
  308.                     {
  309.                         theDocHandle=(MovieDocHndl)GetWRefCon((WindowPtr)window);
  310.                         switch (gKeyMode)
  311.                         {
  312.                             case graphix:
  313.                                 VideoGraphicsMode(window,false);
  314.                             break;
  315.                         }
  316.                         ModifierTrackMode(window);
  317.                     }
  318.                     gKeyMode = modifierTrax;
  319.                 break;
  320.             }
  321.             DoAdjustKeyMode();
  322.         break;
  323.         /* Options menu selected. These options set the playback options of the movie.
  324.            The available options are currently looping and play every frame. Note
  325.            that the Play Every Frame option mutes any active audio tracks when selected. */
  326.         case mOptions:
  327.             switch (menuItem)
  328.             {
  329.                 case iEveryFrame:
  330.                     sPlayAllFrames = SetPlayAllFrames(sPlayAllFrames);
  331.                 break;
  332.                 case iLoopMovie:
  333.                     sLoopMovie = SetLoopMovie(sLoopMovie);
  334.                 break;
  335.             }
  336.             DoAdjustOptions(sPlayAllFrames,sLoopMovie);
  337.         break;
  338.     }
  339.     HiliteMenu(0);        // Unhighlight what MenuSelect (or MenuKey) hilited.
  340. }
  341.  
  342.  
  343. // Adjust the file menubar items
  344.  
  345. #pragma segment Menu
  346. void    DoAdjustFileMenu(void)
  347. {
  348.     MenuHandle            menu;
  349.  
  350.     /* Enable the Open and Close File menu items dependent
  351.      on whether a window is currently open */
  352.     menu = GetMenuHandle(mFile);
  353.     if (gMovieOpen)
  354.     {
  355.         DisableItem(menu,iOpen);
  356.         EnableItem(menu,iClose);
  357.     }
  358.     else if (!gMovieOpen)
  359.     {
  360.         EnableItem(menu,iOpen);
  361.         DisableItem(menu,iClose);
  362.     }
  363.     return;
  364. }
  365.  
  366.  
  367. /* Adjust the Edit menu bar items. Currently as these items
  368.    are disabled as they're not supported. */
  369.  
  370. #pragma segment Menu
  371. void    DoAdjustEditMenu(void)
  372. {
  373.     MenuHandle        menu;
  374.     short            i;
  375.     
  376.     menu = GetMenuHandle(mEdit);
  377.     for (i = iUndo; i <= iPaste; ++i)
  378.         DisableItem(menu, i);
  379.     return;
  380. }
  381.  
  382.  
  383. /* Sets the Keying Mode menu items. This is responsible for setting
  384.    check marks against the currently selected transfer method and
  385.    the position of the movie relative to the background. It also
  386.    enables or disables the movie position items when the graphix
  387.    and modifierTrax methods are used, as these both display in the foreground. */
  388.  
  389. #pragma segment Menu
  390. void    DoAdjustKeyMode(void)
  391. {
  392.     MenuHandle        menu;
  393.     short            itemMark;
  394.     
  395.     menu = GetMenuHandle(mMode);
  396.     switch (gKeyMode)
  397.     {
  398.         case transparentMode:
  399.             GetItemMark(menu,iTransparent,&itemMark);
  400.             if (itemMark == 0x00)
  401.             {
  402.                 SetItemMark(menu,iTransparent,0x12);
  403.                 SetItemMark(menu,iGraphix,0x00);
  404.                 SetItemMark(menu,iModifier,0x00);
  405.                 EnableItem(menu,iMovieBack);
  406.                 EnableItem(menu,iMovieFront);
  407.             }
  408.         break;
  409.         case graphix:
  410.             GetItemMark(menu,iGraphix,&itemMark);
  411.             if (itemMark == 0x00)
  412.             {
  413.                 SetItemMark(menu,iTransparent,0x00);
  414.                 SetItemMark(menu,iGraphix,0x12);
  415.                 SetItemMark(menu,iModifier,0x00);
  416.                 DisableItem(menu,iMovieBack);
  417.                 DisableItem(menu,iMovieFront);
  418.             }
  419.         break;
  420.         case modifierTrax:
  421.             GetItemMark(menu,iModifier,&itemMark);
  422.             if (itemMark == 0x00)
  423.             {
  424.                 SetItemMark(menu,iTransparent,0x00);
  425.                 SetItemMark(menu,iGraphix,0x00);
  426.                 SetItemMark(menu,iModifier,0x12);
  427.                 DisableItem(menu,iMovieBack);
  428.                 DisableItem(menu,iMovieFront);
  429.             }
  430.         break;
  431.     }
  432.     GetItemMark(menu,iMovieBack,&itemMark);
  433.     if (gMovieBackGrnd)
  434.     {
  435.         if (itemMark == 0x00)
  436.         {
  437.             SetItemMark(menu,iMovieBack,0x12);
  438.             SetItemMark(menu,iMovieFront,0x00);
  439.         }
  440.     }
  441.     else
  442.     {
  443.         if (itemMark != 0x00)
  444.         {
  445.             SetItemMark(menu,iMovieBack,0x00);
  446.             SetItemMark(menu,iMovieFront,0x12);
  447.         }
  448.     }
  449.     return;
  450. }
  451.  
  452.  
  453.  
  454. /* DoAdjustOptions() adjusts the Options menu items and 
  455.    placed / removes check marks against these items when
  456.    they're selected / deselected. */
  457.  
  458. #pragma segment Menu
  459. void DoAdjustOptions(Boolean playAllFrames, Boolean loopMovie)
  460. {
  461.     MenuHandle        menu;
  462.     
  463.     menu = GetMenuHandle(mOptions);
  464.     if (playAllFrames)
  465.         SetItemMark(menu,iEveryFrame,0x12);
  466.     else
  467.         SetItemMark(menu,iEveryFrame,0x00);
  468.     if (loopMovie)
  469.         SetItemMark(menu,iLoopMovie,0x12);
  470.     else
  471.         SetItemMark(menu,iLoopMovie,0x00);
  472.     return;
  473. }